﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.House.Entity;
using Chenxin.Lib.Utility;
using System.Text;

namespace Chenxin.Lib.House.BLL
{
    /// <summary>
    /// 业务逻辑类House_DataCollection 的摘要说明。
    /// </summary>
    public class House_DataCollection
    {

        public House_DataCollection()
        { }
        #region  成员方法
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.House.DAL.House_DataCollection.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.House.Entity.House_DataCollection> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.House.DAL.House_DataCollection.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.House.Entity.House_DataCollection> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.House.Entity.House_DataCollection> modelList = new List<Chenxin.Lib.House.Entity.House_DataCollection>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.House.Entity.House_DataCollection model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.House.Entity.House_DataCollection();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["UserId"].ToString() != "")
                    {
                        model.UserId = new Guid(dt.Rows[n]["UserId"].ToString());
                    }
                    model.UserCode = dt.Rows[n]["UserCode"].ToString();
                    model.UserName = dt.Rows[n]["UserName"].ToString();
                    if (dt.Rows[n]["CityId"].ToString() != "")
                    {
                        model.CityId = new Guid(dt.Rows[n]["CityId"].ToString());
                    }
                    model.CityName = dt.Rows[n]["CityName"].ToString();
                    if (dt.Rows[n]["CompanyId"].ToString() != "")
                    {
                        model.CompanyId = new Guid(dt.Rows[n]["CompanyId"].ToString());
                    }
                    model.CompanyName = dt.Rows[n]["CompanyName"].ToString();
                    if (dt.Rows[n]["AreaId"].ToString() != "")
                    {
                        model.AreaId = new Guid(dt.Rows[n]["AreaId"].ToString());
                    }
                    model.AreaName = dt.Rows[n]["AreaName"].ToString();
                    if (dt.Rows[n]["DepartId"].ToString() != "")
                    {
                        model.DepartId = new Guid(dt.Rows[n]["DepartId"].ToString());
                    }
                    model.DepartName = dt.Rows[n]["DepartName"].ToString();
                    if (dt.Rows[n]["ValidCount_Sale"].ToString() != "")
                    {
                        model.ValidCount_Sale = int.Parse(dt.Rows[n]["ValidCount_Sale"].ToString());
                    }
                    if (dt.Rows[n]["ValidCount_Rent"].ToString() != "")
                    {
                        model.ValidCount_Rent = int.Parse(dt.Rows[n]["ValidCount_Rent"].ToString());
                    }
                    if (dt.Rows[n]["RespiteCount_Sale"].ToString() != "")
                    {
                        model.RespiteCount_Sale = int.Parse(dt.Rows[n]["RespiteCount_Sale"].ToString());
                    }
                    if (dt.Rows[n]["RespiteCount_Rent"].ToString() != "")
                    {
                        model.RespiteCount_Rent = int.Parse(dt.Rows[n]["RespiteCount_Rent"].ToString());
                    }
                    if (dt.Rows[n]["HouseTraceCount_Sale"].ToString() != "")
                    {
                        model.HouseTraceCount_Sale = int.Parse(dt.Rows[n]["HouseTraceCount_Sale"].ToString());
                    }
                    if (dt.Rows[n]["HouseTraceCount_Rent"].ToString() != "")
                    {
                        model.HouseTraceCount_Rent = int.Parse(dt.Rows[n]["HouseTraceCount_Rent"].ToString());
                    }
                    if (dt.Rows[n]["HouseTraceCount_SaleRent"].ToString() != "")
                    {
                        model.HouseTraceCount_SaleRent = int.Parse(dt.Rows[n]["HouseTraceCount_SaleRent"].ToString());
                    }
                    if (dt.Rows[n]["CustomerTraceCount_Rent"].ToString() != "")
                    {
                        model.CustomerTraceCount_Rent = int.Parse(dt.Rows[n]["CustomerTraceCount_Rent"].ToString());
                    }
                    if (dt.Rows[n]["CustomerTraceCount_Sale"].ToString() != "")
                    {
                        model.CustomerTraceCount_Sale = int.Parse(dt.Rows[n]["CustomerTraceCount_Sale"].ToString());
                    }
                    if (dt.Rows[n]["SystemHouseTraceCount"].ToString() != "")
                    {
                        model.SystemHouseTraceCount = int.Parse(dt.Rows[n]["SystemHouseTraceCount"].ToString());
                    }
                    if (dt.Rows[n]["SystemCustomerTraceCount"].ToString() != "")
                    {
                        model.SystemCustomerTraceCount = int.Parse(dt.Rows[n]["SystemCustomerTraceCount"].ToString());
                    }
                    if (dt.Rows[n]["KeyCount"].ToString() != "")
                    {
                        model.KeyCount = int.Parse(dt.Rows[n]["KeyCount"].ToString());
                    }
                    if (dt.Rows[n]["MarkedCount"].ToString() != "")
                    {
                        model.MarkedCount = int.Parse(dt.Rows[n]["MarkedCount"].ToString());
                    }
                    if (dt.Rows[n]["ViewCount"].ToString() != "")
                    {
                        model.ViewCount = int.Parse(dt.Rows[n]["ViewCount"].ToString());
                    }
                    if (dt.Rows[n]["HouseCustomerCount"].ToString() != "")
                    {
                        model.HouseCustomerCount = int.Parse(dt.Rows[n]["HouseCustomerCount"].ToString());
                    }
                    if (dt.Rows[n]["DemandCustomerCount"].ToString() != "")
                    {
                        model.DemandCustomerCount = int.Parse(dt.Rows[n]["DemandCustomerCount"].ToString());
                    }
                    if (dt.Rows[n]["BuyCount_Flat"].ToString() != "")
                    {
                        model.BuyCount_Flat = int.Parse(dt.Rows[n]["BuyCount_Flat"].ToString());
                    }
                    if (dt.Rows[n]["BuyCount_Business"].ToString() != "")
                    {
                        model.BuyCount_Business = int.Parse(dt.Rows[n]["BuyCount_Business"].ToString());
                    }
                    if (dt.Rows[n]["BuyCount_Office"].ToString() != "")
                    {
                        model.BuyCount_Office = int.Parse(dt.Rows[n]["BuyCount_Office"].ToString());
                    }
                    if (dt.Rows[n]["RentCount_Flat"].ToString() != "")
                    {
                        model.RentCount_Flat = int.Parse(dt.Rows[n]["RentCount_Flat"].ToString());
                    }
                    if (dt.Rows[n]["RentCount_Business"].ToString() != "")
                    {
                        model.RentCount_Business = int.Parse(dt.Rows[n]["RentCount_Business"].ToString());
                    }
                    if (dt.Rows[n]["RentCount_Office"].ToString() != "")
                    {
                        model.RentCount_Office = int.Parse(dt.Rows[n]["RentCount_Office"].ToString());
                    }
                    if (dt.Rows[n]["ViewPhoneCount"].ToString() != "")
                    {
                        model.ViewPhoneCount = int.Parse(dt.Rows[n]["ViewPhoneCount"].ToString());
                    }
                    if (dt.Rows[n]["AddTime"].ToString() != "")
                    {
                        model.AddTime = DateTime.Parse(dt.Rows[n]["AddTime"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }


        public DataSet GetListByParams(System.Collections.Hashtable htParams)
        {

            int PageIndex = 1;
            int PageSize = 10;
            string TableName = "House_DataCollection Left Join v_Hr_DepartmentInfo ";
            TableName = TableName + " ON (House_DataCollection.DepartId = v_Hr_DepartmentInfo.DepartId) ";
            string FieldList = "ObjectId,ObjectName,ObjectType,ValidCount_Sale,ValidCount_Rent,ValidCount_Rent,";
            FieldList = FieldList + "RespiteCount_Sale,RespiteCount_Rent,HouseTraceCount_Sale,HouseTraceCount_Rent,";
            FieldList = FieldList + "HouseTraceCount_SaleRent,CustomerTraceCount_Rent,CustomerTraceCount_Sale,";
            FieldList = FieldList + "SystemHouseTraceCount,SystemCustomerTraceCount,KeyCount,MarkedCount,ViewCount,";
            FieldList = FieldList + "HouseCustomerCount,DemandCustomerCount,BuyCount_Flat,BuyCount_Business,";
            FieldList = FieldList + "BuyCount_Office,RentCount_Flat,RentCount_Business,RentCount_Office,";
            FieldList = FieldList + "ViewPhoneCount";

            string TableFieldList = ",";
            TableFieldList = TableFieldList + "Sum(ValidCount_Sale) As ValidCount_Sale,";
            TableFieldList = TableFieldList + "Sum(ValidCount_Rent) As ValidCount_Rent,";
            TableFieldList = TableFieldList + "Sum(RespiteCount_Sale) As RespiteCount_Sale,";
            TableFieldList = TableFieldList + "Sum(RespiteCount_Rent) As RespiteCount_Rent,";
            TableFieldList = TableFieldList + "Sum(HouseTraceCount_Sale) As HouseTraceCount_Sale,";
            TableFieldList = TableFieldList + "Sum(HouseTraceCount_Rent) As HouseTraceCount_Rent,";
            TableFieldList = TableFieldList + "Sum(HouseTraceCount_SaleRent) As HouseTraceCount_SaleRent,";
            TableFieldList = TableFieldList + "Sum(CustomerTraceCount_Rent) As CustomerTraceCount_Rent,";
            TableFieldList = TableFieldList + "Sum(CustomerTraceCount_Sale) As CustomerTraceCount_Sale,";
            TableFieldList = TableFieldList + "Sum(SystemHouseTraceCount) As SystemHouseTraceCount,";
            TableFieldList = TableFieldList + "Sum(SystemCustomerTraceCount) As SystemCustomerTraceCount,";
            TableFieldList = TableFieldList + "Sum(KeyCount) As KeyCount,";
            TableFieldList = TableFieldList + "Sum(MarkedCount) As MarkedCount,";
            TableFieldList = TableFieldList + "Sum(ViewCount) As ViewCount,";
            TableFieldList = TableFieldList + "Sum(HouseCustomerCount) As HouseCustomerCount,";
            TableFieldList = TableFieldList + "Sum(DemandCustomerCount) As DemandCustomerCount,";
            TableFieldList = TableFieldList + "Sum(BuyCount_Flat) As BuyCount_Flat,";
            TableFieldList = TableFieldList + "Sum(BuyCount_Business) As BuyCount_Business,";
            TableFieldList = TableFieldList + "Sum(BuyCount_Office) As BuyCount_Office,";
            TableFieldList = TableFieldList + "Sum(RentCount_Flat) As RentCount_Flat,";
            TableFieldList = TableFieldList + "Sum(RentCount_Business) As RentCount_Business,";
            TableFieldList = TableFieldList + "Sum(RentCount_Office) As RentCount_Office,";
            TableFieldList = TableFieldList + "Sum(ViewPhoneCount) As ViewPhoneCount ";

            string OrderField = "";
            string GroupField = "";

            System.Text.StringBuilder StrWhere = new System.Text.StringBuilder();

            StrWhere.Append("IsOpen = 1 AND IsCalculate = 1 AND DepartKind <> '总部'");

            if (htParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(htParams["PageIndex"]);
            }
            if (htParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(htParams["PageSize"]);
            }

            //起始时间
            if (htParams.ContainsKey("StartDate"))
            {
                StrWhere.AppendFormat(" And House_DataCollection.AddTime >= '{0}'", htParams["StartDate"]);
            }
            //结束时间
            if (htParams.ContainsKey("EndDate"))
            {
                StrWhere.AppendFormat(" And House_DataCollection.AddTime <= '{0} 23:59:59'", htParams["EndDate"]);
            }
            //城市
            if (htParams.ContainsKey("CityId"))
            {
                StrWhere.AppendFormat(" And House_DataCollection.CityId='{0}' ", htParams["CityId"]);

                GroupField = "House_DataCollection.CompanyId,House_DataCollection.CompanyName";
                OrderField = "House_DataCollection.CompanyName";
                TableFieldList = "House_DataCollection.CompanyId As ObjectId,House_DataCollection.CompanyName As ObjectName,'CompanyId' As ObjectType" + TableFieldList;
            }
            //公司
            if (htParams.ContainsKey("CompanyId"))
            {
                StrWhere.AppendFormat(" And House_DataCollection.CompanyId='{0}' ", htParams["CompanyId"]);
                StrWhere.Append(" And EXISTS(");
                StrWhere.Append("Select 1 from v_Hr_DepartmentInfo M ");
                StrWhere.Append("WHERE House_DataCollection.AreaId=M.DepartId ");
                StrWhere.Append("And M.IsOpen = 1 ");
                StrWhere.Append(") ");

                GroupField = " AreaId,AreaName";
                OrderField = "AreaName";
                TableFieldList = "AreaId As ObjectId,AreaName As ObjectName,'AreaId' As ObjectType" + TableFieldList;
            }
            //商圈
            if (htParams.ContainsKey("AreaId"))
            {
                StrWhere.AppendFormat(" And AreaId='{0}' ", htParams["AreaId"]);

                GroupField = "House_DataCollection.DepartId,House_DataCollection.DepartName";
                OrderField = "House_DataCollection.DepartName";
                TableFieldList = "House_DataCollection.DepartId As ObjectId,House_DataCollection.DepartName As ObjectName,'DepartId' As ObjectType" + TableFieldList;
            }
            //部门
            if (htParams.ContainsKey("DepartId"))
            {
                StrWhere.AppendFormat(" And House_DataCollection.DepartId='{0}' ", htParams["DepartId"]);
                StrWhere.Append(" And EXISTS(");
                StrWhere.Append("Select 1 from  Chenxin_Main.dbo.Hr_UserInfo M ");
                StrWhere.Append("WHERE House_DataCollection.UserId=M.UserId ");
                StrWhere.Append("And M.IsVirtual=0 ");
                StrWhere.Append("And M.Status=1 ");
                StrWhere.Append("And M.Validate=1 ");
                StrWhere.Append(") ");
                GroupField = "House_DataCollection.UserId,House_DataCollection.UserName";
                OrderField = "House_DataCollection.UserName";
                TableFieldList = "House_DataCollection.UserId As ObjectId,House_DataCollection.UserName As ObjectName,'UserId' As ObjectType" + TableFieldList;
            }

            //权限
            if (htParams.ContainsKey("Permit"))
            {
                string strPermit = htParams["Permit"].ToString();
                //所有权限
                if (strPermit.Equals(Constants.Permit_ViewAllArchivement))
                {

                    GroupField = "House_DataCollection.CityId,House_DataCollection.CityName";
                    OrderField = "House_DataCollection.CityName";
                    TableFieldList = "House_DataCollection.CityId As ObjectId,House_DataCollection.CityName As ObjectName,'CityId' As ObjectType" + TableFieldList;
                }
                //城市权限
                else if (strPermit.Equals(Constants.Permit_ViewCityArchivement))
                {
                    StrWhere.AppendFormat(" And House_DataCollection.CityId='{0}' ", htParams["CurCityId"]);

                    GroupField = "House_DataCollection.CompanyId,House_DataCollection.CompanyName";
                    OrderField = "House_DataCollection.CompanyName";
                    TableFieldList = "House_DataCollection.CompanyId As ObjectId,House_DataCollection.CompanyName As ObjectName,'CompanyId' As ObjectType" + TableFieldList;
                }
                //公司权限
                else if (strPermit.Equals(Constants.Permit_ViewCompanyArchivement))
                {
                    StrWhere.AppendFormat(" And House_DataCollection.CompanyId='{0}' ", htParams["CurCompanyId"]);
                    StrWhere.Append(" And EXISTS(");
                    StrWhere.Append("Select 1 from v_Hr_DepartmentInfo M ");
                    StrWhere.Append("WHERE House_DataCollection.AreaId=M.DepartId ");
                    StrWhere.Append("And M.IsOpen = 1 ");
                    StrWhere.Append(") ");

                    GroupField = "AreaId,AreaName";
                    OrderField = "AreaName";
                    TableFieldList = "AreaId As ObjectId,AreaName As ObjectName,'AreaId' As ObjectType" + TableFieldList;
                }
                //部门权限
                else if (strPermit.Equals(Constants.Permit_ViewDepartArchivement))
                {
                    StrWhere.Append(" And EXISTS(");
                    StrWhere.Append("Select 1 from V_Struct_UserDepartRelation M ");
                    StrWhere.Append("WHERE House_DataCollection.DepartId=M.DepartId ");
                    StrWhere.Append("And ViewBusiness=1 ");
                    StrWhere.AppendFormat("And M.Userid= '{0}' ", htParams["CurUserId"]);
                    StrWhere.Append(") ");

                    GroupField = " House_DataCollection.DepartId,House_DataCollection.DepartName";
                    OrderField = "House_DataCollection.DepartName";
                    TableFieldList = "House_DataCollection.DepartId As ObjectId,House_DataCollection.DepartName As ObjectName,'DepartId' As ObjectType" + TableFieldList;
                }
                else if (strPermit.Equals("PersonalPermit"))
                {
                    StrWhere.AppendFormat(" And House_DataCollection.UserId='{0}' ", htParams["CurUserId"]);

                    GroupField = " House_DataCollection.UserId,House_DataCollection.UserName";
                    OrderField = "House_DataCollection.UserName";
                    TableFieldList = "House_DataCollection.UserId As ObjectId,House_DataCollection.UserName As ObjectName,'UserId' As ObjectType" + TableFieldList;
                }
  
            }

            DataSet ds = Chenxin.Lib.House.DAL.House_DataCollection.GetList(PageIndex, PageSize, OrderField, TableName, StrWhere.ToString(), GroupField, FieldList, TableFieldList, true);
            return ds;
        }

        /// <summary>
        /// 营业数据汇总
        /// </summary>
        /// <param name="htParams">查询条件集合</param>
        /// <returns>汇总集</returns>
        public DataSet GetCollection(int NodeType, Guid ObjectId, DateTime StartDate, DateTime EndDate)
        {
            DataSet ds = Chenxin.Lib.House.DAL.House_DataCollection.GetCollection(NodeType, ObjectId, StartDate, EndDate);
            return ds;
        }
        #endregion  成员方法
    }
}

